from datetime import datetime
from app import db
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(50), unique=True, nullable=False)
    password_hash = db.Column(db.String(100), nullable=False)
    role = db.Column(db.String(20), nullable=False)
    department = db.Column(db.String(50))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)
        
    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

class WorkflowTemplate(db.Model):
    """工作流模板表，用于存储BPMN流程定义"""
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text)
    bpmn_xml = db.Column(db.Text, nullable=False)  # BPMN XML内容
    version = db.Column(db.Integer, default=1)
    status = db.Column(db.String(20), default='draft')  # draft, published
    created_by = db.Column(db.Integer, db.ForeignKey('user.id'))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    creator = db.relationship('User', backref='created_templates')
    instances = db.relationship('WorkflowInstance', backref='template', lazy='dynamic')

class WorkflowInstance(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    workflow_type = db.Column(db.String(50), nullable=False)
    initiator_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    current_state = db.Column(db.String(50))
    status = db.Column(db.String(20), default='pending')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    # 添加模板关联
    template_id = db.Column(db.Integer, db.ForeignKey('workflow_template.id'))
    # 添加流程变量
    variables = db.Column(db.JSON)
    completed_at = db.Column(db.DateTime)
    
    initiator = db.relationship('User', backref='workflows')

class WorkflowTask(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    instance_id = db.Column(db.Integer, db.ForeignKey('workflow_instance.id'), nullable=False)
    task_name = db.Column(db.String(50), nullable=False)
    assignee_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    status = db.Column(db.String(20), default='pending')
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    completed_at = db.Column(db.DateTime)
    # 添加BPMN相关字段
    task_def_key = db.Column(db.String(100))  # BPMN中的任务ID
    form_key = db.Column(db.String(100))      # 关联的表单键
    candidate_groups = db.Column(db.String(200))  # 候选组（部门/角色）
    
    instance = db.relationship('WorkflowInstance', backref='tasks')
    assignee = db.relationship('User', backref='assigned_tasks')

class WorkflowComment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    task_id = db.Column(db.Integer, db.ForeignKey('workflow_task.id'), nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    comment = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    task = db.relationship('WorkflowTask', backref='comments')
    user = db.relationship('User', backref='comments')

class FormDefinition(db.Model):
    """表单定义表，用于存储表单结构"""
    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String(100), nullable=False, unique=True)
    name = db.Column(db.String(100), nullable=False)
    schema = db.Column(db.JSON, nullable=False)  # JSON格式的表单结构
    version = db.Column(db.Integer, default=1)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)

class WorkflowHistory(db.Model):
    """工作流历史记录，扩展自评论功能"""
    id = db.Column(db.Integer, primary_key=True)
    instance_id = db.Column(db.Integer, db.ForeignKey('workflow_instance.id'), nullable=False)
    task_id = db.Column(db.Integer, db.ForeignKey('workflow_task.id'))
    activity_id = db.Column(db.String(100))  # BPMN中的活动ID
    activity_name = db.Column(db.String(100))
    activity_type = db.Column(db.String(50))
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    action = db.Column(db.String(50), nullable=False)  # complete, claim, reject等
    comment = db.Column(db.Text)
    variables = db.Column(db.JSON)  # 记录变量变更
    timestamp = db.Column(db.DateTime, default=datetime.utcnow)
    
    instance = db.relationship('WorkflowInstance', backref='history')
    task = db.relationship('WorkflowTask', backref='history')
    user = db.relationship('User', backref='workflow_history') 